<template>
    <div class="con">
        <div class="info1" style="margin-top:24px">
            <dl>
                <dd>手机号</dd>
                <dt>
                   <p>{{phone}}</p>
                </dt>
            </dl>
            <dl class="last">
                <dd>验证码</dd>
                <dt>
                    <input
                        type="text"
                        pattern="[0-9]*"
                        placeholder="请输入验证码"
                        style="margin-right:80px;width:132px"
                        v-model="authCode"
                    >
                    <span @click="send()" id="yzm">获取验证码</span>
                </dt>
            </dl>
        </div>
        <div class="info1" style="margin-top:14px">
            <dl>
                <dd>新密码</dd>
                <dt>
                    <input type="password" placeholder="请输入新密码 (密码为8-20位字母加数字)" v-model="password">
                </dt>
            </dl>
            <dl>
                <dd>确认新密码</dd>
                <dt>
                    <input type="password" placeholder="请再次输入设置的新密码" v-model="password1">
                </dt>
            </dl>
        </div>

        <div class="btn" @click="ok()">
            <button>完成</button>
        </div>
    </div>
</template>

<script>
export default {
    name: "updatephone",
    data() {
        return {
            phone: "",
            authCode: "",
            timestamp: "",
            password:"",
            password1:""
        };
    },
    methods: {
        send() {
            var myreg = /^[1][0-9]{10}$/;
            if (this.phone == "") {
                this.$toast("请输入手机号");
            } else if (!myreg.test(this.phone)) {
                this.$toast("请填写正确的手机号");
            } else {
                var yzm = document.getElementById("yzm");

                var clock = "";
                var nums = 60;
                yzm.disabled = true; //将按钮置为不可点击
                yzm.style.opacity = "0.5";
                yzm.innerHTML = nums + "s";
                clock = setInterval(doLoop, 1000); //一秒执行一次
                function doLoop() {
                    nums--;
                    if (nums > 0) {
                        yzm.innerHTML = nums + "s";
                    } else {
                        clearInterval(clock); //清除js定时器
                        yzm.disabled = false;
                        yzm.style.opacity = "1";

                        yzm.innerHTML = "获取验证码";
                        nums = 60; //重置时间
                    }
                }
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/app/authCode/getCode?appid=web.2019&format=json&timestamp=${
                        this.timestamp
                    }`,
                    data: {
                        authType: 6, // 和发送验证码里面的 authType 类似
                        phone: this.phone
                    }
                })
                    .then(response => {
                        console.log(response);
                        if (response.data.success == 1) {
                            this.$toast(response.data.message);
                        } else {
                            this.$toast(response.data.message);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        this.$toast("连接服务器异常");
                    });
            }
        },
         ok() {
            var myreg = /^[1][0-9]{10}$/;
            var pwdreg = /^([a-zA-Z]+[0-9]+|[0-9]+[a-zA-Z]+)*$/;
            if (this.phone == "") {
                this.$toast("请输入手机号");
            } else if (!myreg.test(this.phone)) {
                this.$toast("请填写正确的手机号");
            } else if (this.authCode == "") {
                this.$toast("请输入验证码");
            } else if (this.password == "") {
                this.$toast("请输入密码");
            } else if (this.password.length < 8 || this.password.length > 20) {
                this.$toast("请输入8-20为密码");
            } else if (!pwdreg.test(this.password)) {
                this.$toast("请输入字母加数字");
            } else if (this.password1 == "") {
                this.$toast("请输入确认新密码");
            } else if (this.password !== this.password1 ) {
                this.$toast("两次密码不一致");
            } else {
                var md5pwd = this.md5(
                    `${this.md5(this.password)}${this.password}`
                );
                this.axios({
                    method: "post",
                    url: `${
                        this.siteUrl
                    }/app/user/setPassword?appid=web.2019&format=json&timestamp=${
                        this.timestamp
                    }`,
                    data: {
                        loginName: this.phone,
                        authCode: this.authCode,
                        password: md5pwd
                    }
                })
                    .then(response => {
                        console.log(response);
                        if (response.data.success == 1) {
                            this.$toast("修改成功");
                            this.$router.push({ name: "my" });
                        } else {
                            this.$toast(response.data.message);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                        this.$toast("连接服务器异常");
                    });
            }
        },
    },
    mounted() {
        this.timestamp = Date.parse(new Date());
        this.phone=localStorage.getItem("loginphone")
    }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.con {
    width: 100%;
    min-height: 100vh;
    background: #f9f9f9;
    overflow: hidden;
}

.con .info1 {
    background: #fff;
}

.con .info1 dl {
    height: 60px;
    border-bottom: 1px solid #ececec;
    width: 90%;
    margin-left: 5%;
}
.con .info1 dl:last-child {
    border: none;
}
.con .info1 dd {
    line-height: 60px;
    color: #2d3659;
    font-weight: 200;
    float: left;
}
.con .info1 dt {
    float: right;
    width: 200px;
    height: 60px;
    line-height: 60px;
    position: relative;
    text-align: right;
    color: #2d3659;
   
}
.con .info1 span {
    position: absolute;
    right: -10px;
    top: 0px;
    color: #ffae4e;
    width: 80px;
    text-align: center;
}

.con .info1 dt input {
    text-align: right;
    width: 200px;
}

.btn {
    width: 90%;
    height: 48px;
    line-height: 48px;
    background: #ffae4e;
    text-align: center;
    border-radius: 25px;
    box-shadow: 0px 10px 30px 0px #fbc27f;
    margin-left: 5%;
    margin-top: 24px;
}
.btn button {
    width: 100%;
    font-size: 17px;
    color: #fff;
    background: transparent;
}

input::-webkit-input-placeholder {
    color: #e9eaee;
    font-size: 14px;
}
</style>
